package org.grow.loginto.config;

import com.querydsl.jpa.impl.JPAQueryFactory;
import org.grow.loginto.filter.LeaveCountFilter;
import org.grow.loginto.filter.LeaveNumberFilter;
import org.grow.loginto.filter.TestFilter;
import org.grow.loginto.security.Hot;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.access.AccessDecisionManager;
import org.springframework.security.access.AccessDecisionVoter;
import org.springframework.security.access.vote.AffirmativeBased;
import org.springframework.security.access.vote.RoleVoter;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.access.intercept.FilterSecurityInterceptor;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import javax.persistence.EntityManager;
import javax.servlet.Filter;
import java.util.Arrays;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Author: xwg
 * @CreateDate: 21-6-2
 */
@Configuration
public class CommonConfig {
    @Bean
    public FilterRegistrationBean<Filter> corsBean(){
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.setAllowCredentials(true);
        corsConfiguration.setMaxAge(36000L);
        corsConfiguration.addAllowedOriginPattern("*");
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.setAllowedMethods
                (Stream.of("POST","GET","DELETE","PUT","OPTIONS")
                        .collect(Collectors.toList()));
//        CharacterEncodingFilter
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**",corsConfiguration);
        CorsFilter corsFilter = new CorsFilter(source);
        FilterRegistrationBean<Filter> bean = new FilterRegistrationBean<>();
        bean.setFilter(corsFilter);
        bean.setOrder(-1000);
        return bean;
    }


    @Bean
    public PasswordEncoder passwordEncoder(){

        return new PasswordEncoder() {
            @Override
            public String encode(CharSequence charSequence) {
                //不做任何加密处理
                return charSequence.toString();
            }

            @Override
            public boolean matches(CharSequence charSequence, String s) {
                //charSequence是前端传过来的密码，s是数据库中查到的密码
                if (charSequence.toString().equals(s)) {
                    return true;
                }
                return false;
            }
        };
    }

    @Bean
    public FilterRegistrationBean<Filter> testFilterBean(TestFilter testFilter){
        FilterRegistrationBean<Filter> bean = new FilterRegistrationBean<>();
        bean.setFilter(testFilter);
        bean.setOrder(999);
        return bean;
    }
    @Bean
    public JPAQueryFactory jpaQueryFactory(EntityManager entityManager){
        return new JPAQueryFactory(entityManager);
    }

//    @Bean
//    public AccessDecisionManager accessDecisionManager(RoleVoter roleVoter){
//        AffirmativeBased affirmativeBased = new AffirmativeBased(
//                Arrays.asList(roleVoter));
//        return affirmativeBased;
//    }

//    @Bean
//    public FilterSecurityInterceptor filterSecurityInterceptor(Hot hot,AccessDecisionManager accessDecisionManager){
//        FilterSecurityInterceptor filterSecurityInterceptor = new FilterSecurityInterceptor();
//        filterSecurityInterceptor.setSecurityMetadataSource(hot);
//        filterSecurityInterceptor.setAccessDecisionManager(accessDecisionManager);
//        return filterSecurityInterceptor;
//    }

//    @Bean
//    public AccessDecisionVoter<Object> roleVoter(){
//        RoleVoter roleVoter = new RoleVoter();
//        roleVoter.setRolePrefix("");
//        return roleVoter;
//    }
//    @Bean
//    public FilterRegistrationBean<Filter> countLeaveFilter(LeaveCountFilter leaveCountFilter){
//        FilterRegistrationBean<Filter> bean = new FilterRegistrationBean<>();
//        bean.setFilter(leaveCountFilter);
//        bean.setOrder(22);
//        return bean;
//    }
//    @Autowired
//    private LeaveNumberFilter leaveNumberFilter;
//    @Bean
//    public FilterRegistrationBean<Filter> numberLeaveFilter(){
//        FilterRegistrationBean<Filter> f = new FilterRegistrationBean<>();
//        f.setFilter(leaveNumberFilter);
//        f.addUrlPatterns("/*/leave/*");
//        f.setOrder(33);
//        return f;
//    }
}
